package com.kehutong.user.app;

import com.kehutong.common.DoveClient;
import com.kehutong.common.enums.HouseType;
import com.kehutong.common.enums.OperateType;
import com.kehutong.common.enums.ResidentType;
import com.kehutong.common.util.AppDataPvUtils;
import com.kehutong.common.util.Token;
import com.kehutong.contact.entity.Resident;
import com.kehutong.contact.entity.ResidentHouse;
import com.kehutong.contact.enums.BindType;
import com.kehutong.contact.service.ResidentAssetsService;
import com.kehutong.contact.service.ResidentService;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONArray;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.orm.jdbc.Page;
import org.coraframework.orm.jdbc.execute.entity.FindArray;
import org.coraframework.orm.jdbc.execute.entity.FindPage;
import org.coraframework.util.Objects;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户资产管理
 *
 * @author: liukunlong
 * @date: 2021-07-27 10:01
 */
@MvcAction
@ReqMapping("/kht-bin/crm/user/assets")
public class UserAssetsAction {
    private static final Logger logger = LoggerFactory.getLogger(UserAssetsAction.class);

    @Inject
    private ResidentService residentService;

    @Inject
    private ResidentAssetsService residentAssetsService;

    @Inject
    private JdbcSession jdbcSession;

    @Inject
    private DoveClient doveClient;

    /**
     * 住户业主房屋分页列表
     *
     * @param jsonObject
     * @return
     * @throws Exception
     */
    @ReqMapping("/house/page")
    public Object housePage(Token token, JSONObject jsonObject) throws Exception {
        if (Objects.isNull(token)) {
            return Result.unlogin();
        }
        jsonObject.put("houseType", HouseType.HOUSE.getId());
        jsonObject.put("type", ResidentType.OWNER.getId());
        jsonObject.put("communityNos",AppDataPvUtils.getAppCommunityDataPv(token,jsonObject));

        return residentService.getResidentHouseList(jsonObject);
    }

    /**
     * 住户业主房屋住户分页列表
     *
     * @param jsonObject
     * @return
     * @throws Exception
     */
    @ReqMapping("/house/resident/page")
    public Object houseResidentPage(Token token, JSONObject jsonObject) throws Exception {
        if (Objects.isNull(token)) {
            return Result.unlogin();
        }
        List<String> bindTypeList = new ArrayList<>();
        bindTypeList.add(BindType.NO_BIND.getId());
        bindTypeList.add(BindType.BIND.getId());
        FindArray<ResidentHouse> findResidentOwnerHouse = jdbcSession.findArray(ResidentHouse.class)
                .eq(jsonObject,"residentNo")
                .eq("type",ResidentType.OWNER)
                .eq("houseType", HouseType.HOUSE)
                .in("bindType", bindTypeList)
                .eq("deleted", false);
        //多小区数据权限过滤
        JSONArray communityNos=AppDataPvUtils.getAppCommunityDataPv(token,jsonObject);
        if(Objects.nonEmpty(communityNos)){
            findResidentOwnerHouse.in("communityNo",communityNos);
        }
        List<ResidentHouse> residentOwnerHouseList = findResidentOwnerHouse.exe();
        List<String> houseNos = residentOwnerHouseList.stream().map(ResidentHouse::getHouseNo).collect(Collectors.toList());
        if(Objects.isEmpty(houseNos)){
            return Result.success();
        }
        Page<ResidentHouse> result = null;
        FindPage<ResidentHouse> find = jdbcSession.findPage(ResidentHouse.class)
                .in("houseNo",houseNos)
                .in("bindType", bindTypeList)
                .eq("houseType", HouseType.HOUSE)
                .noEq("type", ResidentType.OWNER)
                .eq("deleted", false)
                .order("createTime", "desc");
        result = find.page(jsonObject).exe();
        //构建数据集合业主身份下面的已认证住户数量
        if (Objects.nonNull(result)) {
            List<ResidentHouse> residentHouseList = result.getList();
            for (ResidentHouse residentHouse : residentHouseList) {
                Resident resident = residentService.getResidentById(residentHouse.getResidentNo());
                residentHouse.setRealname(resident.getRealname());
                residentHouse.setMobile(resident.getMobile());
                residentHouse.setGender(resident.getGender());
                residentHouse.setCardNo(resident.getCardNo());
                residentHouse.setBirthDay(resident.getBirthDay());
            }
        }
        return result;
    }


    /**
     * 住户业主商铺分页列表
     *
     * @param jsonObject
     * @return
     * @throws Exception
     */
    @ReqMapping("/shop/page")
    public Object shopPage(Token token, JSONObject jsonObject) throws Exception {
        if (Objects.isNull(token)) {
            return Result.unlogin();
        }
        jsonObject.put("houseType", HouseType.SHOP.getId());
        jsonObject.put("type", ResidentType.OWNER.getId());
        jsonObject.put("communityNos",AppDataPvUtils.getAppCommunityDataPv(token,jsonObject));
        return residentService.getResidentHouseList(jsonObject);
    }


    /**
     * 住户业主车位分页列表
     *
     * @param jsonObject
     * @return
     * @throws Exception
     */
    @ReqMapping("/parking/page")
    public Object parkingPage(Token token, JSONObject jsonObject) throws Exception {
        if (Objects.isNull(token)) {
            return Result.unlogin();
        }
        jsonObject.put("houseType", HouseType.PARKING.getId());
        jsonObject.put("type", ResidentType.OWNER.getId());
        jsonObject.put("communityNos",AppDataPvUtils.getAppCommunityDataPv(token,jsonObject));
        return residentService.getResidentHouseList(jsonObject);
    }

    /**
     * 绑定住户资产列表
     *
     * @param jsonObject
     * @return
     * @throws Exception
     */
    @ReqMapping("/bind")
    public Object bindUserAssets(Token token, JSONObject jsonObject) throws Exception {
        if (Objects.isNull(token)) {
            return Result.unlogin();
        }
        //资产编号
        String assetsNo = jsonObject.getString("assetsNo");
        //资产类型
        String assetsType = jsonObject.getString("assetsType");
        //住户编号
        String residentNo = jsonObject.getString("residentNo");
        logger.info("bindResidentAssets.param:{}", jsonObject.toJSONString());
        if (Objects.isEmpty(assetsNo) || Objects.isEmpty(assetsType) && Objects.isEmpty(residentNo)) {
            return Result.error(0, "必填参数为空.");
        }
        String companyName = token.getCompanyName();
        jsonObject.put("companyName", companyName);
        jsonObject.put("companyNo",token.getCompanyNo());
        return residentAssetsService.bindResidentOwnerAssets(jsonObject);
    }


    /***
     * 根据条件删除住户房屋
     * 1.业主列表删除
     * 2.亲属列表删除
     * 3.租户列表删除
     * @param jsonObject 查询条件
     * @return 操作结果
     * @throws Exception 操作异常
     */
    //@HasPermission("crm:residentHouse:edit")
    @ReqMapping("/delete")
    public Object delete(JSONObject jsonObject) throws Exception {
        //住户编号
        String residentNo = jsonObject.getString("residentNo");

        //资产编号
        String assetsNo = jsonObject.getString("assetsNo");
        //资产类型
        String assetsType = jsonObject.getString("assetsType");

        jsonObject=new JSONObject();
        jsonObject.put("id",residentNo);
        jsonObject.put("houseNo",assetsNo);
        jsonObject.put("type","1");
        residentService.deleteResidentHouse(jsonObject);
        return Result.success();
    }


}
